package text5;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;

class index extends JFrame{
    int num;
    public index(){
        input();
        frame();
        this.setVisible(true);
    }
    public void frame(){
        this.setTitle("进制转换");
        this.setAlwaysOnTop(true);//最上方
        this.setDefaultCloseOperation(3);//关闭程序
        this.setLocationRelativeTo(null);
        this.pack();
    }
    public void input(){
        JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER, 10, 30));
        JLabel l1 = new JLabel("输入十进制数");
        l1.setFont(new Font("宋体", 1, 12));
        TextField f1 = new TextField(12);
        p1.add(l1);
        p1.add(f1);
        this.add(p1, BorderLayout.NORTH);
        Box vbox = Box.createVerticalBox();
        JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        TextArea area = new TextArea("",5,25, TextArea.SCROLLBARS_VERTICAL_ONLY);
        JLabel l2 = new JLabel("请选择进制: ");
        panel.add(area);
        panel.add(l2);
        vbox.add(panel);
        JPanel p2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JCheckBox b1 = new JCheckBox("二进制");
        JCheckBox b2 = new JCheckBox("八进制");
        JCheckBox b3 = new JCheckBox("十六进制");
        JCheckBox b4 = new JCheckBox("十进制");
        p2.add(b1);
        p2.add(b2);
        p2.add(b3);
        p2.add(b4);
        JPanel p3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JLabel l3 = new JLabel("请选择进制:");
        ButtonGroup group = new ButtonGroup();
        JRadioButton b5 = new JRadioButton("二进制");
        JRadioButton b6 = new JRadioButton("八进制");
        JRadioButton b7 = new JRadioButton("十六进制");
        JRadioButton b8 = new JRadioButton("十进制");
        p3.add(l3);
        group.add(b5);
        group.add(b6);
        group.add(b7);
        group.add(b8);
        p3.add(b5);
        p3.add(b6);
        p3.add(b7);
        p3.add(b8);
        vbox.add(p2);
        vbox.add(p3);
        this.add(vbox);
       f1.addTextListener(new TextListener() {
           @Override
           public void textValueChanged(TextEvent e) {
               if (b5.isSelected()){
                   Long num = new Long(f1.getText());
                   area.setText(Long.toBinaryString(num)+"\n");
               }
               if (b6.isSelected()){
                   Long num = new Long(f1.getText());
                   area.setText(Long.toOctalString(num));
               }
               if (b7.isSelected()){
                   Long num = new Long(f1.getText());
                   area.setText(Long.toHexString(num));
               }
               if (b8.isSelected()){
                   area.setText(f1.getText());
               }
           }
       });

    }
}
public class Num4 {
    public static void main(String[] args) {
        index index = new index();
    }

}
